package com.shzu.jwc.ty.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shzu.jwc.ty.bean.UserTeacherInfo;
import com.shzu.jwc.ty.common.Msg;
import com.shzu.jwc.ty.service.UserTeacherInfoservice;

@Controller
@RequestMapping("/jwc/teacherinfo")
public class JwcTeacherInfocontroller {

	@Autowired
	UserTeacherInfoservice userTeacherInfoservice;
	
	@RequestMapping("/teacher.action")
	public String showteacher(@RequestParam(value="pn",defaultValue="1") Integer pn,Model model)
	{
		PageHelper.startPage(pn, 20);
		PageInfo<UserTeacherInfo> pageinfo=new PageInfo<UserTeacherInfo>(userTeacherInfoservice.getteacher());
		model.addAttribute("pageinfo", pageinfo);
		return "jwc/teacherinfo/teacher";
	}
	
	@RequestMapping("/searchteacher.action")
	public String searchteacher(@RequestParam("userTeacherInfoName2")String userTeacherInfoName,@RequestParam(value="pn",defaultValue="1") Integer pn,Model model) {
		PageHelper.startPage(pn, 20);
		PageInfo<UserTeacherInfo> pageinfo=new PageInfo<UserTeacherInfo>(userTeacherInfoservice.serach(userTeacherInfoName));
		model.addAttribute("pageinfo", pageinfo);
		return "jwc/teacherinfo/teacher";
	}
	
	@ResponseBody
	@RequestMapping("/insert.action")
	public Msg insert(UserTeacherInfo userTeacherInfo) {
		try {
			if(userTeacherInfoservice.isextis(userTeacherInfo.getUserTeacherInfoNum())>1)
				return Msg.error("该老师已存在");
			userTeacherInfoservice.insert(userTeacherInfo);
			return Msg.success("添加成功");
		}catch(Exception ex) {
			return Msg.error("添加失败,"+ex.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping("/delete.action")
	public Msg delete(@RequestParam("id")Integer userTeacherInfoId) {
		try {
			userTeacherInfoservice.delete(userTeacherInfoId);
			return Msg.success("操作成功");
		}catch(Exception ex) {
			return Msg.error("操作失败,"+ex.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping("/getoneteacher.action")
	public Msg getoneteacher(@RequestParam("id")Integer userTeacherInfoId) {
		return Msg.success("操作成功").add("teacher", userTeacherInfoservice.getone(userTeacherInfoId));
	}
	
	@ResponseBody
	@RequestMapping("/repass.action")
	public Msg repass(@RequestParam("id")Integer userTeacherInfoId) {
		try {
			userTeacherInfoservice.repass(userTeacherInfoId);
			return Msg.success("操作成功");
		}catch(Exception ex) {
			return Msg.error("操作失败,"+ex.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping("/updatefun.action")
	public Msg updatefun(UserTeacherInfo userTeacherInfo) {
		try {
			if(userTeacherInfoservice.isextis(userTeacherInfo.getUserTeacherInfoNum())>1)
				return Msg.error("该老师已存在");
			userTeacherInfoservice.update(userTeacherInfo);
			return Msg.success("操作成功");
		} catch(Exception ex) {
			return Msg.error("操作失败,"+ex.getMessage());
		}
	}
	
}
